package com.servlet.demo;

import com.servlet.demo.core.UserCoreService;
import com.servlet.demo.dto.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

public class UserServlet extends HttpServlet {

    private UserCoreService userCoreService = new UserCoreService();

    /**
     * 请求连接路由
     */
    private static Map<String, BiConsumer<HttpServletRequest,HttpServletResponse>> routerMap = new HashMap<String, BiConsumer<HttpServletRequest,HttpServletResponse>>();
    @Override
    public void init() {
        routerMap.put("list.do", this::userList);
        routerMap.put("detail.do", this::userDetail);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 截取排除顶层Servlet定义的url，当前顶层定义的url是/user/。举例/user/list，截取结果就是list
        String uri = request.getRequestURI().substring(6);
        // detail是有参数的，那么久需要判断?,从?开始，删除后面的全部，留下连接url本身作为路由判断条件。举例/user/detail?id=12，截取结果就是detail
        if (uri.indexOf("?") != -1) {
            uri = uri.substring(0, uri.indexOf("?"));
        }
        // 当前是demo效果，所以以上规则就可以匹配到路由list和detail了

        BiConsumer<HttpServletRequest, HttpServletResponse> router = routerMap.get(uri);
        if (router == null) {
            System.out.println("未找到匹配路由信息");
        } else {
            router.accept(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * 用户列表
     * @param request   request
     * @param response  response
     */
    public void userList(HttpServletRequest request, HttpServletResponse response) {
        List<User> list = userCoreService.getUserList();
        request.setAttribute("list", list);
        try {
            request.getRequestDispatcher("/user/list.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户详情
     * @param request   request
     * @param response  response
     */
    public void userDetail(HttpServletRequest request, HttpServletResponse response) {
        String id =  request.getParameter("id");
        if ("".equals(id)) {
            // 这里需要抓取错误的原因是，重定向的连接可能不存在
            try {
                // 重定向的url连接前，必须加入当前web程序的路劲名
                response.sendRedirect(request.getContextPath() + "/user/list");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        User user = userCoreService.getUserById(Long.valueOf(id));
        if (user == null) {
            try {
                response.getWriter().println("当前指定id，获取用户信息错误");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        request.setAttribute("user", user);
        try {
            request.getRequestDispatcher("/loginInfo.jsp").forward(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}
